package com.changgou.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.Result;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.page.service.PageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {
    @Autowired
    private TemplateEngine templateEngine;//thymeleaf 模板引擎
    @Autowired
    private SpuFeign spuFeign;
    @Autowired
    private SkuFeign skuFeign;
    @Autowired
    private CategoryFeign categoryFeign;
    @Value("${pagepath}")
    private String pagepath;
    @Override
    public void generateHtml(String spuId) {
        //2 获取到context对象，存储商品的相关数据
        Context context = new Context();
        Map<String,Object> itemData=this.getItemData(spuId);
        context.setVariables(itemData);
        PrintWriter writer = null;
        try {
            //3页面文件夹存不存在
            File dir = new File(pagepath);
            if (!dir.exists()){
                dir.mkdir();
            }
            //页面文件
            File pageFile = new File(dir + "/" + spuId + ".html");
            writer = new PrintWriter(pageFile);

            //1. 使用引擎生成页面
            /**
             * String template 模板名称 item
             * IContext context 数据Map
             * Writer writer 输出流
             */
            templateEngine.process("item", context,writer);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //关闭流
            if (writer!=null){
                writer.close();
            }
        }

    }

    private Map<String, Object> getItemData(String spuId) {
        Map<String,Object> resultMap = new HashMap<>();
        //封装需要的数据
        //spu
        Spu spu= spuFeign.findSpuById(spuId).getData();
        resultMap.put("spu",spu);
        //sku
        List<Sku> skuList = skuFeign.findListBySpuId(spuId);
        resultMap.put("skuList",skuList);
        //category
        Category category = categoryFeign.findById(spu.getCategory1Id()).getData();
        resultMap.put("category1",category);
        Category category2 = categoryFeign.findById(spu.getCategory2Id()).getData();
        resultMap.put("category2",category2);
        Category category3 = categoryFeign.findById(spu.getCategory3Id()).getData();
        resultMap.put("category3",category3);
        //imageList
        //拿图片的集合。以逗号分隔
        if (spu!=null){
            if (StringUtils.isNotEmpty(spu.getImages())){
                resultMap.put("imageList",spu.getImages().split(","));
            }
        }
        // 获取商品规格信息
        resultMap.put("specificationList", JSON.parseObject(spu.getSpecItems(),Map.class));
        return resultMap;
    }
}
